// pages/login/login.js
import Toast from '@vant/weapp/toast/toast'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    account:'',
    password:'',
    isPrivacy:false,
    showPrivacy: false,
    privacyContractName:'',
    isPrivacyOne:false,
    value1:0,
  },
  exitMiniProgram(){
    wx.exitMiniProgram()
  },
  handleAgreePrivacyAuthorization() {
    this.setData({
      showPrivacy:false
    })
  },
  handleOpenPrivacyContract() {
    wx.openPrivacyContract({
      success: () => {}, // 打开成功
      fail: () => {}, // 打开失败
      complete: () => {}
    })
  },
  consent(e){
    if(!wx.getStorageSync('isPrivacy')){
      console.log(1);
      Toast({
        message:'请先完成阅读',
        duration:3000
     });
    
    }else{
      this.setData({
        isPrivacy:e.detail
      })
    }
    
  },
  //登录
  submit(e){
    let that = this
    wx.getPrivacySetting({
      success: res => {
        console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
        if (res.needAuthorization) {
          // 需要弹出隐私协议
          this.setData({
            showPrivacy: true
          })
        } else {
          console.log(that.data.isPrivacy,'登录',this.data.account,'---',this.data.password);
    if(that.data.isPrivacy==false){
      that.setData({
        isPrivacyOne:true
      })
      if(that.data.isPrivacyOne){
        setTimeout(()=>{
          that.setData({
            isPrivacyOne:false
          })
        },2000)
      }
    }else{
      that.setData({
        isPrivacyOne:false
      })
      wx.redirectTo({
        url: '/pages/welcome/welcome',
      })
      // wx.switchTab({
      //   url: '/pages/survey/survey',
      // })
    }
          // 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用已声明过的隐私接口
          // wx.getUserProfile()
          // wx.chooseMedia()
          // wx.getClipboardData()
          // wx.startRecord()
        }
      },
      fail: () => {},
      complete: () => {}
    })
    
  },
  powInput(e){
    this.setData({
      password:e.detail
    })
  },
  actInput(e){
    this.setData({
      account:e.detail
    })
  },
  // 隐私协议
  privacyTo(){
    let that = this
    wx.navigateTo({
      url: '/pages/privacy/privacy?isPrivacy='+(that.data.isPrivacy?1:0),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(object) {
    let that = this
    if(object){
      that.setData({
        isPrivacy:true
      })
      console.log(object.isPrivacy);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})